package com.ksyun.seckill.controller.inner;

import com.ksyun.seckill.bo.AppInfo;
import com.ksyun.seckill.rest.RestResult;
import com.ksyun.seckill.service.AppInfoService;
import com.ksyun.seckill.util.TokenBucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 */
@RestController
@RequestMapping(value = "/inner/ops")
public class InnerOpsController {

    @Autowired
    private AppInfoService appInfoService;

    private static TokenBucket tokenBucket = new TokenBucket();
    /**
     * 针对这个接口做限流
     *
     * 使用令牌桶进行限流
     * @return
     */
    @RequestMapping(value = "/health/app/info", method = {RequestMethod.GET})
    @ResponseBody
    public RestResult getAppInfo() {
        if (tokenBucket.getToken()) {
            AppInfo appInfo = appInfoService.build();
            return RestResult.success().data(appInfo);
        } else {
            return RestResult.failure().msg("访问人数过多，请稍后在访问！");
        }
    }
}